package com.ewzj.bigdata.app.ossupload;

import android.content.Context;
import android.util.Log;
import android.util.TimeUtils;

import com.alibaba.sdk.android.oss.ClientException;
import com.alibaba.sdk.android.oss.OSSClient;
import com.alibaba.sdk.android.oss.ServiceException;
import com.alibaba.sdk.android.oss.callback.OSSCompletedCallback;
import com.alibaba.sdk.android.oss.callback.OSSProgressCallback;
import com.alibaba.sdk.android.oss.common.auth.OSSCustomSignerCredentialProvider;
import com.alibaba.sdk.android.oss.common.utils.OSSUtils;
import com.alibaba.sdk.android.oss.internal.OSSAsyncTask;
import com.alibaba.sdk.android.oss.model.GetObjectRequest;
import com.alibaba.sdk.android.oss.model.GetObjectResult;
import com.alibaba.sdk.android.oss.model.OSSRequest;
import com.alibaba.sdk.android.oss.model.PutObjectRequest;
import com.alibaba.sdk.android.oss.model.PutObjectResult;
import com.ewzj.bigdata.app.constants.AppConstants;
import com.ewzj.bigdata.app.utils.DateStyle;
import com.ewzj.bigdata.app.utils.DateUtil;
import com.jess.arms.base.App;
import com.jess.arms.utils.ArmsUtils;

import java.io.File;
import java.util.Date;

/**
 * Created by wangzheng on 2018/1/11.
 */

public class CustomSignSample extends BaseSamples {

    String path;
    OSSAsyncTask task;
    public CustomSignSample() {

    }
    public CustomSignSample(String path) {
        this.path = path;
    }
    public void customSignDown(Context ctx,String object,OSSCompletedCallback ossCompletedCallback) {
        OSSCustomSignerCredentialProvider provider = new OSSCustomSignerCredentialProvider() {
            @Override
            public String signContent(String content) {

                // 此处本应该是客户端将contentString发送到自己的业务服务器,然后由业务服务器返回签名后的content。关于在业务服务器实现签名算法
                // 详情请查看http://help.aliyun.com/document_detail/oss/api-reference/access-control/signature-header.html。客户端
                // 的签名算法实现请参考OSSUtils.sign(accessKey,screctKey,content)
                String signedString = OSSUtils.sign(AppConstants.OSS_KEY, AppConstants.OSS_SCRET, content);
                return signedString;
            }
        };

        OSSClient tClient = new OSSClient(ctx, AppConstants.ENDPOINT, provider);

        GetObjectRequest get = new GetObjectRequest(AppConstants.OSS_BUCKET, object);
        get.setCRC64(OSSRequest.CRC64Config.YES);
        get.setProgressListener(new OSSProgressCallback<GetObjectRequest>() {
            @Override
            public void onProgress(GetObjectRequest request, long currentSize, long totalSize) {
                Log.d("GetObject", "currentSize: " + currentSize + " totalSize: " + totalSize);

            }
        });
        task = tClient.asyncGetObject(get, ossCompletedCallback);
    }
    public void customSignUpload(Context ctx,String filepath,OSSProgressCallback ossProgressCallback,OSSCompletedCallback ossCompletedCallback) {
        OSSCustomSignerCredentialProvider provider = new OSSCustomSignerCredentialProvider() {
            @Override
            public String signContent(String content) {

                // 此处本应该是客户端将contentString发送到自己的业务服务器,然后由业务服务器返回签名后的content。关于在业务服务器实现签名算法
                // 详情请查看http://help.aliyun.com/document_detail/oss/api-reference/access-control/signature-header.html。客户端
                // 的签名算法实现请参考OSSUtils.sign(accessKey,screctKey,content)
                String signedString = OSSUtils.sign(AppConstants.OSS_KEY, AppConstants.OSS_SCRET, content);
                return signedString;
            }
        };

        OSSClient tClient = new OSSClient(ctx, AppConstants.ENDPOINT, provider);
        File file=new File(filepath);
        String objectkey= DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM_SS)+file.getName();
        PutObjectRequest put = new PutObjectRequest(AppConstants.OSS_BUCKET,objectkey,filepath);
        put.setProgressCallback(ossProgressCallback);
        task=tClient.asyncPutObject(put, ossCompletedCallback);
    }

    public void cancel(){
        if (task!=null){
            task.cancel();
        }
    }
}
